//
//  BySuperLottoAddMinusView.m
//  caipiao_ios
//
//  Created by mc on 2018/8/3.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "BySuperLottoAddMinusView.h"
//10 + 20 + 5 + 20 + 5 + 30 + 5 + 20 + 5 + 20 = 140
@implementation BySuperLottoAddMinusView

- (id)init{
    self = [super init];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        UILabel *buyLabel = [[UILabel alloc] init];
        [self addSubview:buyLabel];
        [buyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(10);
            make.centerY.equalTo(self).offset(0);
            make.size.mas_equalTo(CGSizeMake(20, 20));
        }];
        buyLabel.text = @"追";
        buyLabel.textColor = BY_TEXT_COLOR;
        buyLabel.textAlignment = NSTextAlignmentCenter;
        buyLabel.font = FONT(14);
        
        UIButton *minusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:minusBtn];
        [minusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(buyLabel.mas_right).offset(5);
            make.centerY.equalTo(self).offset(0);
            make.size.mas_equalTo(CGSizeMake(23, 23));
        }];
        [minusBtn setImage:[UIImage imageNamed:@"jian"] forState:UIControlStateNormal];
        minusBtn.tag = 100;
        
        UITextField *countLabel = [[UITextField alloc] init];
        [self addSubview:countLabel];
        [countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(minusBtn.mas_right).offset(5);
            make.centerY.equalTo(self).offset(0);
            make.size.mas_equalTo(CGSizeMake(40, 20));
        }];
        countLabel.text = @"1";
        countLabel.textColor = BY_TEXT_COLOR;
        countLabel.textAlignment = NSTextAlignmentCenter;
        countLabel.font = FONT(14);
        countLabel.keyboardType = UIKeyboardTypeNumberPad;
        countLabel.delegate = self;
        countLabel.layer.borderColor = BY_LINE_COLOR.CGColor;
        countLabel.layer.borderWidth = 1;
        self.countLabel = countLabel;
        
        UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:addBtn];
        [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(countLabel.mas_right).offset(5);
            make.centerY.equalTo(self).offset(0);
            make.size.mas_equalTo(CGSizeMake(23, 23));
        }];
        [addBtn setImage:[UIImage imageNamed:@"jia"] forState:UIControlStateNormal];
        addBtn.tag = 101;
        
        UILabel *timesLabel = [[UILabel alloc] init];
        [self addSubview:timesLabel];
        [timesLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(addBtn.mas_right).offset(5);
            make.centerY.equalTo(self).offset(0);
            make.size.mas_equalTo(CGSizeMake(20, 20));
        }];
        timesLabel.text = @"期";
        timesLabel.textColor = BY_TEXT_COLOR;
        timesLabel.textAlignment = NSTextAlignmentCenter;
        timesLabel.font = FONT(14);
        
        
        [minusBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [addBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        self.timesLabel = timesLabel;
        self.buyLabel = buyLabel;
        
    }
    return self;
}


- (void)btnAction:(UIButton *)btn{
   if (btn.tag == 100){
        if (self.countLabel.text.integerValue > 1) {
            self.countLabel.text = [NSString stringWithFormat:@"%ld", self.countLabel.text.integerValue - 1];
            if (self.updateBlock) {
                self.updateBlock();
            }
        }
    }else if (btn.tag == 101){
        self.countLabel.text = [NSString stringWithFormat:@"%ld", self.countLabel.text.integerValue + 1];
        if (self.updateBlock) {
            self.updateBlock();
        }
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.text.integerValue < 1) {
        textField.text = @"1";
        [ByCoreToolAlert showOnlyText:@"最小为1"];
        if (self.updateBlock) {
            self.updateBlock();
        }
    }else{
        if (self.updateBlock) {
            self.updateBlock();
        }
    }
    
}


@end
